package com.txy.domain.strategy.service.rule.chain.impl;

import com.txy.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.txy.domain.strategy.repository.IStrategyRepository;
import com.txy.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.txy.domain.strategy.service.rule.chain.factory.LogicChainFactory;
import com.txy.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component("rule_blacklist")
public class RuleBlackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;

    @Override
    public LogicChainFactory.LogicChainRes logic(Long strategyId, String userId) {
        log.info("规则过滤-黑名单, strategyId={}, userId={}", strategyId, userId);
        // 查strategy_rule表获取规则rule_value
        String strategyRuleValue = repository.queryStrategyRuleValue(strategyId, LogicChainFactory.LogicModel.RULE_BLACKLIST.getCode());

        String[] split = strategyRuleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(split[0]);
        String userIdList = split[1];
        for (String ruleUserId : userIdList.split(Constants.SPLIT)) {
            if (userId.equals(ruleUserId)) {
                log.info("规则过滤-黑名单接管, award={}", awardId);
                return LogicChainFactory.LogicChainRes.builder()
                        .ruleLogicCheckTypeVO(RuleLogicCheckTypeVO.TAKE_OVER)
                        .logicModel(LogicChainFactory.LogicModel.RULE_BLACKLIST)
                        .logicChainAward(LogicChainFactory.LogicChainAward
                                .builder()
                                .awardId(awardId)
                                .build())
                        .build();

            }
        }
        log.info("规则过滤-黑名单放行, strategyId={}, userId={}", strategyId, userId);
        return next().logic(strategyId, userId);
    }
}
